/// 回复内容数据结构
class ResponseItem {
  final String responseId;
  final String topicId;
  final String responseText;
  final String contributorId;
  final String contributorDisplayName;
  final String contributorProfileImage;
  final DateTime publishedTime;
  final DateTime lastModifiedTime;
  final int approvalCount;
  final int disapprovalCount;
  final bool isVerifiedResponse;
  final bool isMarkedUseful;

  ResponseItem({
    required this.responseId,
    required this.topicId,
    required this.responseText,
    required this.contributorId,
    required this.contributorDisplayName,
    required this.contributorProfileImage,
    required this.publishedTime,
    required this.lastModifiedTime,
    this.approvalCount = 0,
    this.disapprovalCount = 0,
    this.isVerifiedResponse = false,
    this.isMarkedUseful = false,
  });

  /// 从数据映射构建对象
  factory ResponseItem.fromDataMap(Map<String, dynamic> dataMap) {
    return ResponseItem(
      responseId: dataMap['responseId'] as String,
      topicId: dataMap['topicId'] as String,
      responseText: dataMap['responseText'] as String,
      contributorId: dataMap['contributorId'] as String,
      contributorDisplayName: dataMap['contributorDisplayName'] as String,
      contributorProfileImage: dataMap['contributorProfileImage'] as String,
      publishedTime: DateTime.parse(dataMap['publishedTime'] as String),
      lastModifiedTime: DateTime.parse(dataMap['lastModifiedTime'] as String),
      approvalCount: dataMap['approvalCount'] as int? ?? 0,
      disapprovalCount: dataMap['disapprovalCount'] as int? ?? 0,
      isVerifiedResponse: dataMap['isVerifiedResponse'] as bool? ?? false,
      isMarkedUseful: dataMap['isMarkedUseful'] as bool? ?? false,
    );
  }

  /// 转换为数据映射
  Map<String, dynamic> toDataMap() {
    return {
      'responseId': responseId,
      'topicId': topicId,
      'responseText': responseText,
      'contributorId': contributorId,
      'contributorDisplayName': contributorDisplayName,
      'contributorProfileImage': contributorProfileImage,
      'publishedTime': publishedTime.toIso8601String(),
      'lastModifiedTime': lastModifiedTime.toIso8601String(),
      'approvalCount': approvalCount,
      'disapprovalCount': disapprovalCount,
      'isVerifiedResponse': isVerifiedResponse,
      'isMarkedUseful': isMarkedUseful,
    };
  }

  /// 创建修改版本
  ResponseItem createModifiedVersion({
    String? responseId,
    String? topicId,
    String? responseText,
    String? contributorId,
    String? contributorDisplayName,
    String? contributorProfileImage,
    DateTime? publishedTime,
    DateTime? lastModifiedTime,
    int? approvalCount,
    int? disapprovalCount,
    bool? isVerifiedResponse,
    bool? isMarkedUseful,
  }) {
    return ResponseItem(
      responseId: responseId ?? this.responseId,
      topicId: topicId ?? this.topicId,
      responseText: responseText ?? this.responseText,
      contributorId: contributorId ?? this.contributorId,
      contributorDisplayName:
          contributorDisplayName ?? this.contributorDisplayName,
      contributorProfileImage:
          contributorProfileImage ?? this.contributorProfileImage,
      publishedTime: publishedTime ?? this.publishedTime,
      lastModifiedTime: lastModifiedTime ?? this.lastModifiedTime,
      approvalCount: approvalCount ?? this.approvalCount,
      disapprovalCount: disapprovalCount ?? this.disapprovalCount,
      isVerifiedResponse: isVerifiedResponse ?? this.isVerifiedResponse,
      isMarkedUseful: isMarkedUseful ?? this.isMarkedUseful,
    );
  }

  /// 计算净支持度
  int calculateNetSupport() => approvalCount - disapprovalCount;

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ResponseItem && other.responseId == responseId;
  }

  @override
  int get hashCode => responseId.hashCode;

  @override
  String toString() {
    return 'ResponseItem(id: $responseId, topic: $topicId, contributor: $contributorDisplayName, support: $approvalCount)';
  }
}
